package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gxs.domain.GxsComprehensive;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsMoralityMapper;
import com.ruoyi.gxs.domain.GxsMorality;
import com.ruoyi.gxs.service.IGxsMoralityService;

/**
 * 思想品德变化Service业务层处理
 * 
 * @author 高晓松
 * @date 2024-01-03
 */
@Service
public class GxsMoralityServiceImpl implements IGxsMoralityService 
{
    @Autowired
    private GxsMoralityMapper gxsMoralityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private GxsComprehensiveMapper comprehensiveMapper;

    /**
     * 查询思想品德变化
     * 
     * @param gxsId 思想品德变化主键
     * @return 思想品德变化
     */
    @Override
    public GxsMorality selectGxsMoralityByGxsId(Long gxsId)
    {
        return gxsMoralityMapper.selectGxsMoralityByGxsId(gxsId);
    }

    /**
     * 查询思想品德变化列表
     * 
     * @param gxsMorality 思想品德变化
     * @return 思想品德变化
     */
    @Override
    public List<GxsMorality> selectGxsMoralityList(GxsMorality gxsMorality)
    {
        return gxsMoralityMapper.selectGxsMoralityList(gxsMorality);
    }

    /**
     * 新增思想品德变化
     * 
     * @param gxsMorality 思想品德变化
     * @return 结果
     */
    @Override
    public int insertGxsMorality(GxsMorality gxsMorality)
    {
        //        根据学号查询 昵称、用户ID、部门 并添加到gxsMorality
        SysUser sysUser = sysUserMapper.selectUserByUserName(gxsMorality.getUserName());
        gxsMorality.setNickName(sysUser.getNickName());
        gxsMorality.setUserId(sysUser.getUserId());
        gxsMorality.setDeptId(sysUser.getDeptId());

        //根据新增的专业分数信息 查询综合得分学习成绩
        GxsComprehensive comprehensive = comprehensiveMapper.selectGxsComprehensiveByUserName(gxsMorality.getUserName());
        gxsMorality.setGxsMoralityRecord(comprehensive.getGxsThought());


        //根据加分状态对学习成绩进行加减
        long totalPoints =0L;
        if (gxsMorality.getGxsBonusState() == 0L){
            totalPoints = gxsMorality.getGxsMoralityRecord() + gxsMorality.getGxsScore();
        }
        else {
            totalPoints = gxsMorality.getGxsMoralityRecord() - gxsMorality.getGxsScore();
        }
        gxsMorality.setGxsTotalPoints(totalPoints);
        comprehensive.setGxsThought(totalPoints);
        comprehensiveMapper.updateGxsComprehensive(comprehensive);

        gxsMorality.setCreateTime(DateUtils.getNowDate());
        return gxsMoralityMapper.insertGxsMorality(gxsMorality);
    }

    /**
     * 修改思想品德变化
     * 
     * @param gxsMorality 思想品德变化
     * @return 结果
     */
    @Override
    public int updateGxsMorality(GxsMorality gxsMorality)
    {
        gxsMorality.setUpdateTime(DateUtils.getNowDate());
        return gxsMoralityMapper.updateGxsMorality(gxsMorality);
    }

    /**
     * 批量删除思想品德变化
     * 
     * @param gxsIds 需要删除的思想品德变化主键
     * @return 结果
     */
    @Override
    public int deleteGxsMoralityByGxsIds(Long[] gxsIds)
    {
        return gxsMoralityMapper.deleteGxsMoralityByGxsIds(gxsIds);
    }

    /**
     * 删除思想品德变化信息
     * 
     * @param gxsId 思想品德变化主键
     * @return 结果
     */
    @Override
    public int deleteGxsMoralityByGxsId(Long gxsId)
    {
        return gxsMoralityMapper.deleteGxsMoralityByGxsId(gxsId);
    }
}
